package myExample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.collection.CollectionReader_ImplBase;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceConfigurationException;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;

// Collection Reader

public class FilesCollectionReader extends CollectionReader_ImplBase {
	private String mInput = "InputDirectory";
	private ArrayList<File> mFiles;			
	private int mCurrentIndex;
	
	public void initialize() throws ResourceInitializationException {
		File directory = new File(((String) getConfigParameterValue(mInput)).trim());
		mCurrentIndex = 0;
		// exception
		if (!directory.exists() || !directory.isDirectory()) {
	      throw new ResourceInitializationException(ResourceConfigurationException.DIRECTORY_NOT_FOUND,
	              new Object[] { "InputDirectory", this.getMetaData().getName(), directory.getPath() });
	    }
		
		// read files
		mFiles = new ArrayList<File>();
	    File[] files = directory.listFiles();
	    for (int i = 0; i < files.length; i++) {
	      if (!files[i].isDirectory()) {
	        mFiles.add(files[i]);
	      }
	    }
	}
	
	@Override
	public void getNext(CAS acas) throws IOException, CollectionException {
		// TODO Auto-generated method stub
		JCas jcas;
	    try {
	      jcas = acas.getJCas();
	    } catch (CASException e) {
	      throw new CollectionException(e);
	    }
	    File file = (File) mFiles.get(mCurrentIndex++);
	    FileInputStream input = new FileInputStream(file);
	    BufferedReader reader = new BufferedReader(new InputStreamReader(input,"UTF-8"));
	    // get plain text
	    Html2Text parser = new Html2Text();
	    parser.parse(reader);
	    reader.close();
	    String noHTMLString = parser.getText();
	    jcas.setDocumentText(noHTMLString);
	    
	    SourceDocument src = new SourceDocument(jcas);
	    src.setURI(file.getAbsoluteFile().toURI().toString());
	    src.addToIndexes();
	}

	@Override
	public void close() throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public Progress[] getProgress() {
		return new Progress[] { new ProgressImpl(mCurrentIndex, mFiles.size(), Progress.ENTITIES) };
	}

	@Override
	public boolean hasNext() throws IOException, CollectionException {
		return mCurrentIndex < mFiles.size();
	}
	
}
